package com.xebest.web.controller.storehouse;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.xebest.common.base.controller.BASEMessage;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.config.ParamConfig;
import com.xebest.constant.XeConstans;
import com.xebest.util.DateUtil;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.util.msg.XeVenOrderMsg;
import com.xebest.web.model.StoreDeliveryForm;
import com.xebest.web.model.base.XePageInfo;
import com.xebest.web.service.XeVenStoreDelivService;
import com.xebest.web.view.ErpUserInfo;
import com.xebest.web.view.XeVenDelivDetailView;
import com.xebest.web.view.XeVenStoreDelivView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 出库单
 *
 * @author 杜春静
 */

@RestController
@RequestMapping("delivManager")
public class XeVenStoreDelivController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(XeVenStoreDelivController.class);//日志记录类

    @Autowired
    XeVenStoreDelivService delivService;

    @Autowired
    private ParamConfig paramConfig;

    /**
     * 查询出库单列表
     *
     * @author 杜春静
     */
    @RequestMapping("queryDelivList")
    public BaseResponse queryDelivList(@RequestBody StoreDeliveryForm form) {

        log.info("查询出库单列表请求参数为：{}", JSON.toJSONString(form));
        //1、验证参数
        if (null == form) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }

        //判断时间是否超过3个月
        if (!StringUtils.isEmpty(form.getCreateDateStart()) && !StringUtils.isEmpty(form.getCreateDateEnd())) {
            boolean isOverThree = DateUtil.comparableTime(form.getCreateDateStart(), form.getCreateDateEnd());
            if (!isOverThree) {
                return sendAjaxError(XeVenOrderMsg.TIME_MSG);
            }
            String createDateEnd = form.getCreateDateEnd();
            form.setCreateDateEnd(createDateEnd + " 23:59:59");
        }
        //如果查询条件出库时间为空，则默认为1个月
        if (StringUtils.isEmpty(form.getCreateDateStart()) && StringUtils.isEmpty(form.getCreateDateEnd())) {
            form.setCreateDateStart(DateUtil.getFirstMonth(1));
            form.setCreateDateEnd(DateUtil.getDayLastTime(0));
        }

        if (StringUtils.isEmpty(form.getCreateDateStart()) && !StringUtils.isEmpty(form.getCreateDateEnd())) {

            form.setCreateDateStart(DateUtil.strToNMonth(form.getCreateDateEnd(), -1));
        }
        if (!StringUtils.isEmpty(form.getCreateDateStart()) && StringUtils.isEmpty(form.getCreateDateEnd())) {
            form.setCreateDateEnd(DateUtil.strToNMonth(form.getCreateDateStart(), 1));
        }

        if (null != form.getQueryFlag()) {
            Integer queryFlag = form.getQueryFlag();
            switch (queryFlag) {
                case 1:   //出库单号
                    form.setDeliveryOrder(form.getQueryCondition());
                    ;
                    break;
                case 2:   //关联单号
                    form.setRelationOrder(form.getQueryCondition());
                    break;
                case 3:   //商品名称
                    form.setProName(form.getQueryCondition());
                    break;
                case 4:   //商品货号
                    form.setProNum(form.getQueryCondition());
                    break;
                default:
                    break;
            }
        }

        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        //封装前台数据
        XePageInfo<XeVenStoreDelivView> xePageInfo;
        PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("form", form);
        try {
            List<XeVenStoreDelivView> delivInfoList = this.delivService.listDelivByParam(form);
            xePageInfo = new XePageInfo<XeVenStoreDelivView>(delivInfoList);
            //封装返回参数
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("actionList", delivInfoList);
            map.put("xePageInfo", xePageInfo);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询出库单列表 异常为：{}", e);
            return sendAjaxError();
        }
    }

    /**
     * 查询出库单商品明细
     *
     * @author 杜春静
     */
    @RequestMapping("queryDelivDetailList")
    public BaseResponse queryDelivDetailList(@RequestBody StoreDeliveryForm form) {

        log.info("查询出库单列表请求参数为：{}", JSON.toJSONString(form));
        //1、验证参数
        if (null == form || null == form.getDeliveryOrder()) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        //封装参数
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取供应商code
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        paramMap.put("form", form);
        try {
            List<XeVenDelivDetailView> delivDetailList = this.delivService.listDelivDetailByParam(paramMap);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("actionList", delivDetailList);
            String picServer = paramConfig.getImgUrl() + XeConstans.ERP_PRO_PIC_DIR;//图片服务器地址
            map.put("picServer", picServer);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询出库单商品明细列表,异常信息为{} ", e);
            return sendAjaxError();
        }
    }
}
  
